<?php
/**
 * 创建榜单数据
 * 更新时间：每周一，3点40
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\bigData\BrandRankingTitle as bigBrandRankingTitle;
use Biz\model\wiki\WikiRankingConfig;
use Biz\model\wiki\WikiRankingData;
use Biz\model\wiki\WikiRankingWeek;
use Biz\service\wikiRanking\Ranking;
use Biz\tools\Mail;
use Biz\cache\wiki\WikiRanking as WikiRankingCache;

$start = time();
//创建榜单周期
$dayArr = Ranking::getCreateRankingDay();
$day = $dayArr['day'];
$sourceDay = $dayArr['source_day'];
$now = date('Ymd');
if ($sourceDay != $now) {
    var_dump('当前日期为：' . $now . '不能创建：' . $sourceDay . '的榜单');
    return false;
}
$info = WikiRankingWeek::getByWhere(['ranking_day' => $day]);
if (empty($info)) {
    $result = WikiRankingWeek::insertData(['ranking_day' => $day]);
}
$list = bigBrandRankingTitle::select('*', ['day' => $sourceDay]);
if (empty($list)) {
    var_dump('未查询到榜单数据');
    Mail::send(['tangyiwan@haohaozhu.com', 'caowenxue@haohaozhu.com'], '未查询到榜单数据', '未查询到榜单数据#sql:' . json_encode(bigBrandRankingTitle::log()));
    return false;
}
$rankingIds = array_column($list, 'ranking_id');
$rankingList = WikiRankingConfig::selectByWhere(['id' => $rankingIds], ['id', 'ranking_name']);
$rankingList = array_column($rankingList, null, 'id');
$page = 1;
$listArr = [];
do {
    $listArr = array_slice($list, ($page - 1) * 20, 100);
    if ($listArr) {
        foreach ($listArr as $row) {
            $data = [
                'ranking_day' => $day,
                'ranking_id' => $row['ranking_id'],
                'ranking_name' => isset($rankingList[$row['ranking_id']]) ? $rankingList[$row['ranking_id']]['ranking_name'] : '',
                'user_cnt' => $row['user_like_cnt'],
                'wiki_cnt' => $row['wiki_cnt'],
                'brand_cnt' => $row['brand_cnt'],
                'rank_cnt' => $row['rank_cnt'],
                'addtime' => date('Y-m-d H:i:s'),
                'edittime' => date('Y-m-d H:i:s')
            ];
            WikiRankingData::insertData($data);
            WikiRankingCache::setWikiRankingDataIds($day, $row['ranking_id'], $row['rank_cnt']);
        }
        var_dump('创建榜单数据#page:' . $page);
    }
    $page++;
} while ($listArr);

//检查是否创建数据成功
$check = WikiRankingData::selectCountWhere(['ranking_day' => $day]);
if (!$check) {
    throw new \Exception('未查询到创建的' . $day . '榜单数据');
}

$end = time();
var_dump(date('Y-m-d-H-i-s') . '第' . $day . '期榜单,创建榜单数据,耗时:' . ($end - $start) . '秒，' . (($end - $start) / 60) . '分');
//5秒内